package com.hn.kiss.modules.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hn.kiss.config.common.CommonPage;
import com.hn.kiss.config.common.CommonResult;
import com.hn.kiss.config.common.MyException;
import com.hn.kiss.config.compoent.BaseController;
import com.hn.kiss.config.query.QueryGenerator;
import com.hn.kiss.modules.sys.entity.Role;
import com.hn.kiss.modules.sys.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.List;


@Api(tags = "SysRoleController", description = "角色表管理")
@RestController
@RequestMapping("/sys/role")
@Validated
public class RoleController extends BaseController {

    @Autowired
    IRoleService roleService;

    @ApiOperation("角色表分页列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNo", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/pageInfo")
    public CommonPage<Role> pageInfo(@NotNull Integer pageNo,
                                     @NotNull Integer pageSize,
                                     Role role,
                                     HttpServletRequest request){
        Page<Role> pageInfo = new Page<>(pageNo, pageSize);
        //构造查询条件
        QueryWrapper<Role> qw = QueryGenerator.initQueryWrapper(role, request.getParameterMap());
        Page<Role> page1 = roleService.page(pageInfo, qw);
        return CommonPage.restPage(page1);
    }

    @ApiOperation("角色列表")
    @GetMapping
    public CommonResult<List<Role>> pageInfo(){
        List<Role> list = roleService.list();
        return CommonResult.success(list);
    }

    @ApiOperation("通过主键获取角色表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "code", value = "代码", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("/{code}")
    public CommonResult<Role> getByPk(@NotBlank@PathVariable("code") String code){
        Role role = roleService.getByPk(code);
        return CommonResult.success(role);
    }

    @ApiOperation("新增角色表")
    @PostMapping
    public CommonResult add(@Validated Role role, BindingResult bindingResult) throws MyException {
        //角色代码统一转化为小写
        role.setCode(role.getCode().toLowerCase());
        check(role.getCode());

        boolean b = roleService.save(role);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("修改角色表")
    @PutMapping
    public CommonResult updateByPk(@Validated Role role, BindingResult bindingResult){
        boolean b = roleService.updateByPk(role);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("通过主键删除角色表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "code", value = "代码", required = true, dataType = "String", paramType = "path")
    })
    @DeleteMapping("/{code}")
    public CommonResult removeByPk(@NotBlank@PathVariable("code") String code){
        boolean b = roleService.removeByPk(code);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    /**
     * 校验角色代码是否存在
     * @param code
     * @return
     */
    private boolean check(String code) throws MyException {
        List<Role> list = roleService.list(Wrappers.<Role>lambdaQuery().eq(Role::getCode, code));
        if (list != null && list.size() > 0){
            throw new MyException("角色code已存在");
        }
        return true;
    }

}
